package com.jiaanpei.frameproject.modle.login;

import android.Manifest;
import android.os.Handler;
import android.text.TextUtils;
import android.widget.ImageView;

import com.jiaanpei.frameproject.R;
import com.jiaanpei.frameproject.app.Constants;
import com.jiaanpei.frameproject.base.BaseActivity;
import com.jiaanpei.frameproject.manage.ImageLoaderManager;
import com.jiaanpei.frameproject.manage.Navigate;
import com.jiaanpei.frameproject.manage.UserManager;
import com.jiaanpei.frameproject.modle.bean.base.BaseLoader;
import com.jiaanpei.frameproject.modle.bean.base.BaseRequest;
import com.jiaanpei.frameproject.modle.bean.base.BaseResponse;
import com.jiaanpei.frameproject.modle.bean.base.CallBackListener;
import com.jiaanpei.frameproject.modle.bean.db.User;
import com.jiaanpei.frameproject.modle.bean.login.LoginBean;
import com.jiaanpei.frameproject.modle.bean.login.LoginRequestBean;
import com.jiaanpei.frameproject.modle.user.UserDbManager;
import com.jiaanpei.frameproject.wxapi.WXPayEntryActivity;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.io.File;
import java.util.List;

import butterknife.BindView;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.iv)
    ImageView iv;

    @Override
    protected int getLayout() {
        return R.layout.ac_login;
    }
    @Override
    protected boolean isSupportSwipeBack() {
        return false;
    }
    @Override
    protected void initEventAndData() {
        msgApi = WXAPIFactory.createWXAPI(this, null);
        /**
         * 将app注册到微信
         */
        msgApi.registerApp(Constants.WX_APP_ID);
        getAppUpdateForNet();
        testDB();
        ImageLoaderManager.loadCircleImage(this,"https://ss1.baidu.com/-4o3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=48c0fc3576cb0a469a228d395b62f63e/7dd98d1001e93901650c93f176ec54e737d196f6.jpg",iv);
    }

    private IWXAPI msgApi;
    private void toServer(){
        PayReq req = new PayReq();
//        req.appId = Constants.WX_APP_ID;
//        req.partnerId = docType.getPartnerId();
//        req.prepayId = ""+docType.getPrepayId();
//        req.nonceStr = ""+docType.getNonceStr();
//        req.timeStamp = ""+docType.getTimeStamp();
//        req.packageValue = "Sign=WXPay";
//        req.sign = ""+docType.getSign();
        msgApi.sendReq(req);
    }

    private void testDB() {
        for (int i=0;i<10;i++){
            User user=new User();
            user.setName("www"+i);
            user.setAge(i+"");
            UserDbManager.getInstance(getApplicationContext()).insert(user);
        }
        List<User> users = UserDbManager.getInstance(getApplicationContext()).queryList("");
        WXPayEntryActivity.orderNo ="";//orderPayBean.getBody().getBaseInfo().getOrderNo();
        WXPayEntryActivity.Type = WXPayEntryActivity.BBYP_TYPE;
        toServer();//orderPayBean.getBody().getBaseInfo().getAppSdkParams()
    }

    public void getLoginForNet() {

        LoginRequestBean requestBean = new LoginRequestBean();
//        requestBean.currentVersion=String.valueOf(Utils.getVersionCode(BaseActivity.this));
        BaseRequest baseRequest = new BaseRequest("60");
        baseRequest.setRequestBaseInfo(requestBean);
        BaseLoader.LoginBean(baseRequest, new CallBackListener<LoginBean>(this, true) {
            @Override
            protected void onSuccess(BaseResponse<LoginBean> result) {
                BaseResponse.BaseResponseBody<LoginBean> body = result.body;

                LoginBean baseInfo = body.baseInfo;
                String errCode = result.head.errCode;
                String errMsg = result.head.errMsg;
                if(baseInfo!=null&&errCode.equals("000")){
                    UserManager.saveUser(LoginActivity.this,"","","",mPushAgent);

                }

            }

            @Override
            protected void requestError(String error) {
                super.requestError(error);
            }
        });
    }
}
